package com.ztgf.logistics.api.client.deppon;

import com.alibaba.fastjson.JSON;
import com.ztgf.logistics.models.bo.deppon.LogisticsHttpLogSaveBO;
import com.ztgf.logistics.models.bo.deppon.api.DepponPushOrderBO;
import com.ztgf.logistics.models.service.deppon.ILogisticsDepponOrderService;
import com.ztgf.logistics.models.service.deppon.ILogisticsHttpLogService;
import com.ztgf.logistics.models.service.deppon.ILogisticsOrderService;
import com.ztgf.logistics.models.vo.deppon.api.DepponResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * 用户信息表控制器
 *
 * @author mxli
 * @Date 2020-07-30 12:13:56
 */

@RestController
@RequestMapping("/api/logisticsNotify")
@Slf4j
@Api(value = "PayNotifyController",tags = "物流回调Api接口")
public class LogisticsNotifyController {

    @DubboReference
    private ILogisticsDepponOrderService logisticsDepponOrderService;
    @DubboReference
    private ILogisticsOrderService logisticsOrderService;
    @DubboReference
    private ILogisticsHttpLogService logisticsHttpLogService;

    @PostMapping("/deppon")
    @ApiOperation(value = "德邦回调",notes = "德邦回调")
    public void doDepponNotice(HttpServletRequest request, HttpServletResponse response){

        //时间戳
        String timestamp = request.getParameter("timestamp");
        //摘要
        String digest = request.getParameter("digest");
        //参数
        String params= request.getParameter("params");
        //时间戳验证使用，判断请求是否在有效的时间戳范围内，时间戳范围根据情况自行定义
        long sendTime=Long.parseLong(timestamp);
        long now = System.currentTimeMillis();
        long effectiveTimestamp=1800000;
        String expectDigest= logisticsDepponOrderService.getAlibabaDigest(params, timestamp);
        DepponResultVO resultVO = new DepponResultVO();
        resultVO.setResultCode("1000");
        resultVO.setResult(true);
        resultVO.setReason("成功");
        if(Math.abs(now - sendTime)>effectiveTimestamp){//时间戳验证
            resultVO.setResultCode("1001");
            resultVO.setResult(false);
            resultVO.setReason("时间戳失败");
        }else if (!expectDigest.equals(digest.trim())) {//摘要验证
            resultVO.setResultCode("1002");
            resultVO.setResult(false);
            resultVO.setReason("摘要验证失败");
        }
        //添加日志
        try {
            DepponPushOrderBO bo = JSON.parseObject(params.replaceAll("null", "0"), DepponPushOrderBO.class);
            logisticsOrderService.saveDoNotify(bo);
        } catch (Exception e) {
            // 4 处理响应报文参数
            e.printStackTrace();
            log.error("回调错误", e);
            resultVO.setResultCode("1003");
            resultVO.setResult(false);
            resultVO.setReason("业务处理失败");
        } finally {
            // 5 响应智融平台
            PrintWriter out = null;
            try {
                String result = JSON.toJSONString(resultVO);

                out = response.getWriter();
                out.print(result);
                out.flush();

                LogisticsHttpLogSaveBO saveBO = new LogisticsHttpLogSaveBO();
                saveBO.setBusinessNotes("德邦物流回调");
                saveBO.setParams(params);
                saveBO.setReturnValue(result);
                int status = 1;
                if(resultVO.isResult()){
                    status = 0;
                }
                saveBO.setStatus(status);
                //添加日志
                logisticsHttpLogService.add(saveBO);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (null != out) {
                    out.close();
                }
            }
        }

    }

}
